home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / CHILD.CPP < prev    next >
C/C++ Source or Header  |  1993-06-10  |  6KB  |  202 lines

  1. // child.cpp -- TChildWindow implementation
  2.  
  3. #include "child.h"
  4.  
  5. // Return maximum of a or b
  6. inline long max(long a, long b)
  7. {
  8.   return (a > b) ? a : b;
  9. }
  10.  
  11. // Return minimum of a or b
  12. inline long min(long a, long b)
  13. {
  14.   return (a < b) ? a : b;
  15. }
  16.  
  17. // Construct the main window and add scrollers
  18. TChildWindow::TChildWindow(PTWindowsObject AParent, LPSTR fName)
  19.   : text(250, 0, 125), TWindow(AParent, fName)
  20. {                                             
  21.   Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  22.   strcpy(fileName, fName);
  23.   fontColor = GetSysColor(COLOR_WINDOWTEXT);
  24.   fontPointSize = 10;
  25.   numRows = numCols = 0;
  26.   width = height = 0;
  27.   glyphH = glyphW = 0;
  28.   colSep = rowSep = 0;
  29.   Scroller = new TScroller(this, 1, 1, 0, 0);
  30.   if (Scroller) Scroller->AutoOrg = FALSE;
  31.   if (!ReadFile()) {
  32.     MessageBox(HWindow, fileName, "Error opening file",
  33.       MB_ICONSTOP | MB_OK);
  34.     Status = EM_INVALIDCHILD;
  35.   }
  36. }
  37.  
  38. void TChildWindow::SetupWindow()
  39. {
  40.   int logPixelsY;  // Number of pixels per vertical logical inch
  41.   HDC hDC;         // Device context for obtaining system specs
  42.   hDC = GetDC(HWindow);
  43.   logPixelsY = GetDeviceCaps(hDC, LOGPIXELSY);
  44.   ReleaseDC(HWindow, hDC);
  45.   memset(&font, 0, sizeof(font));
  46.   font.lfHeight = -MulDiv(fontPointSize, logPixelsY, 72);
  47.   font.lfWeight = FW_NORMAL;
  48.   font.lfCharSet = ANSI_CHARSET;
  49.   font.lfOutPrecision = OUT_TT_PRECIS;
  50.   font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  51.   font.lfQuality = PROOF_QUALITY;
  52.   font.lfPitchAndFamily = FIXED_PITCH;
  53.   strcpy(font.lfFaceName, "Courier New");
  54.   hFont = CreateFontIndirect(&font);
  55.   if (hFont == NULL)
  56.     Status = EM_INVALIDCHILD;
  57.   AdjustSettings();
  58.   TWindow::SetupWindow();
  59. }
  60.  
  61. // Read strings from text file into String object array
  62. int TChildWindow::ReadFile()
  63. {
  64.   FILE *f;               // DOS file pointer
  65.   char buffer[BUFSIZE];  // Input buffer
  66.   long len;              // Length of string in buffer
  67.   char *cp;              // Miscellaneous character pointer
  68.   String *sp;            // Pointer to a String object
  69.   f = fopen(fileName, "r");
  70.   if (!f) return FALSE;
  71.   text.flush();
  72.   numRows = numCols = 0;
  73.   while (fgets(buffer, BUFSIZE, f)) {
  74.     cp = strchr(buffer, '\n');
  75.     if (cp)
  76.       *cp = NULL;
  77.     sp = new String(buffer);
  78.     len = (long)strlen(buffer);
  79.     if (len > numCols)
  80.       numCols = len;
  81.     text.add(*sp);
  82.     numRows++;
  83.   }
  84.   fclose(f);
  85.   return TRUE;
  86. }
  87.  
  88. // Adjust document and font settings
  89. void TChildWindow::AdjustSettings()
  90. {
  91.   TEXTMETRIC tm;   // Text (font) information
  92.   HDC hDC;         // Handle to device context
  93.   HFONT hOldFont;  // Handle to old hDC font
  94.   if (hFont == NULL) return;
  95.   hDC = GetDC(HWindow);
  96.   hOldFont = (HFONT)SelectObject(hDC, hFont);
  97.   GetTextMetrics(hDC, &tm);
  98.   glyphW = tm.tmMaxCharWidth;
  99.   glyphH = tm.tmHeight;
  100.   rowSep = tm.tmExternalLeading;
  101.   colSep = GetTextCharacterExtra(hDC);
  102.   SelectObject(hDC, hOldFont);
  103.   width = colSep + ((numCols + 1) * (long)(glyphW + colSep));
  104.   height = rowSep + ((numRows + 1) * (long)(glyphH + rowSep));
  105.   ReleaseDC(HWindow, hDC);
  106. }
  107.  
  108. // Adjust scroll bars to match document and font settings
  109. void TChildWindow::AdjustScroller()
  110. {
  111.   RECT R;     // Rectangle for client area dimensions
  112.   long H, W;  // Document size minus window in display units
  113.   if (numRows <= 0) return;
  114.   GetClientRect(HWindow, &R);
  115.   W = width - (R.right - R.left);
  116.   H = height - (R.bottom - R.top);
  117.   Scroller->SetUnits(glyphW + colSep, glyphH + rowSep);
  118.   Scroller->SetRange(W / Scroller->XUnit, H / Scroller->YUnit);
  119.   SetScrollPos(HWindow, SB_HORZ, Scroller->XPos, TRUE);
  120.   SetScrollPos(HWindow, SB_VERT, Scroller->YPos, TRUE);
  121. }
  122.  
  123. // Select font, size, and style
  124. void TChildWindow::CMFontSelect(RTMessage)
  125. {
  126.   CHOOSEFONT cf;     // Choose-font dialog structure
  127.   LOGFONT tempFont;  // Temporary copy of TFontWin's logical font
  128.   memset(&cf, 0, sizeof(cf));
  129.   tempFont = font;
  130.   cf.lStructSize = sizeof(CHOOSEFONT);
  131.   cf.hwndOwner = HWindow;
  132.   cf.Flags = CF_FIXEDPITCHONLY | CF_EFFECTS | 
  133.     CF_INITTOLOGFONTSTRUCT | CF_BOTH;
  134.   cf.lpLogFont = &tempFont;
  135.   cf.rgbColors = fontColor;
  136.   if (ChooseFont(&cf)) {
  137.     font = *cf.lpLogFont;
  138.     fontColor = cf.rgbColors;
  139.     fontPointSize = cf.iPointSize / 10;
  140.     if (hFont) DeleteObject(hFont);
  141.     hFont = CreateFontIndirect(&font);
  142.     AdjustSettings();
  143.     AdjustScroller();
  144.     InvalidateRect(HWindow, NULL, TRUE);
  145.     UpdateWindow(HWindow);
  146.   }
  147. }
  148.  
  149. // Respond to changes in the child window's size
  150. void TChildWindow::WMSize(RTMessage Msg)
  151. {
  152.   TWindow::WMSize(Msg);
  153.   if (Scroller && Msg.WParam != SIZEICONIC)
  154.     AdjustScroller();
  155. }
  156.  
  157. // Respond to WM_KEYDOWN messages directed to child
  158. void TChildWindow::WMKeyDown(RTMessage msg)
  159. {
  160.   long XLine = Scroller->XLine;
  161.   long YLine = Scroller->YLine;
  162.   long YPage = Scroller->YPage;
  163.   switch (msg.WParam){
  164.     case VK_UP:    Scroller->ScrollBy(0, -YLine ); break;
  165.     case VK_DOWN:  Scroller->ScrollBy(0,  YLine ); break;
  166.     case VK_LEFT:  Scroller->ScrollBy(-XLine, 0 ); break;
  167.     case VK_RIGHT: Scroller->ScrollBy( XLine, 0 ); break;
  168.     case VK_PRIOR: Scroller->ScrollBy(0, -YPage ); break;
  169.     case VK_NEXT:  Scroller->ScrollBy(0,  YPage ); break;
  170.     default: return;
  171.   }
  172.   msg.Result = 0;
  173. }
  174.  
  175. // Paint or repaint the child (document) window contents
  176. void TChildWindow::Paint(HDC PaintDC, PAINTSTRUCT &ps)
  177. {
  178.   int x, y;                  // String coordinates
  179.   int row, col;              // Row and column numbers
  180.   int startRow, endRow;      // Rows in update region
  181.   String *sp;                // String object pointer
  182.   HFONT hOldFont;            // Old font handle
  183.  
  184.   if (numRows <= 0) return;
  185.   SetTextColor(PaintDC, fontColor);
  186.   SetBkColor(PaintDC, GetSysColor(COLOR_WINDOW));
  187.   hOldFont = (HFONT)SelectObject(PaintDC, hFont);
  188.   startRow = (int)max(0,
  189.     1 + (Scroller->YPos + ps.rcPaint.top / Scroller->YUnit - 1));
  190.   endRow = (int)min(numRows,
  191.     1 + (Scroller->YPos + ps.rcPaint.bottom / Scroller->YUnit));
  192.   for (row = startRow; row < endRow; row++) {
  193.     x = colSep + ((Scroller->XUnit * (1 - Scroller->XPos))
  194.       - Scroller->XUnit); 
  195.     y = rowSep + ((Scroller->YUnit * (1 - Scroller->YPos + row))
  196.       - Scroller->YUnit);
  197.     sp = (String *)&text[row];
  198.     TextOut(PaintDC, x, y, *sp, strlen(*sp));
  199.   }
  200.   SelectObject(PaintDC, hOldFont);
  201. }
  202.